package com.ViewModel;



import gueei.binding.Binder;
import gueei.binding.Command;
import gueei.binding.observables.StringObservable;
import gueei.binding.validation.ModelValidator;
import gueei.binding.validation.ValidationResult;
import gueei.binding.validation.validators.EqualsTo;
import gueei.binding.validation.validators.Required;
import gueei.binding.validation.validators.RegexMatch;
import gueei.binding.validation.validators.MinLength;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;

public class LoginViewModel extends Activity {

	 @Required(ErrorMessage="Usuario Es Obligatorio!")
	 @RegexMatch(Pattern = "^[A-Za-z0-9_]{3,8}$", ErrorMessage="Usuario necesita ser entre 3-8 carateres y alfanumerico.")
	 public  StringObservable Usuario = new StringObservable();
	
	 @Required(ErrorMessage="Clave Es Obligatoria!")
	 @MinLength(Length=5, ErrorMessage="Clave Minimo 5 Caracteres!")
	 public  StringObservable  Clave = new StringObservable();
	
	 @Required(ErrorMessage="ConfirmarClave Es Obligatorio!")
	 @EqualsTo(Observable = "Clave", ErrorMessage="Confirmar Clave Debe Ser Igual a Clave")
	 public  StringObservable  ConfirmarClave = new StringObservable();
	
	 
	 
	 
	 public Command Ingresar = new Command(){
	    	public void Invoke(View arg0,Object... arg1){
	    		ingresar();
	    	}
	    	
	    };
	    
	    public void ingresar()
	    {
	    	
	    	Dialog nuevoDialogo= new Dialog(this);
	    	nuevoDialogo = crearDialogoAlerta();
	    	nuevoDialogo.show();
	    }
	    
	    private Dialog crearDialogoAlerta()
	    {
	        AlertDialog.Builder builder = new AlertDialog.Builder(this);
	        builder.setTitle("Informacion");
	        String MensajeDialogo ="" ;
	        ValidationResult result =  ModelValidator.ValidateModel(this);
	        String[] Errores = result.getValidationErrors();
	        for (String error: Errores)
	        {
	        	MensajeDialogo += error.toString()+"\n";
	        }
	        builder.setMessage(MensajeDialogo);
	        return builder.create();
	    }
	 
	    

    @SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	
    	Binder.setAndBindContentView(this, R.layout.login_view, this);
    }
}
